const { src, dest, parallel, series, watch } = require('gulp');

const stream = require('stream');
const through2 = require('through2');

const defineFn = () => {
  return new stream.Transform({
    objectMode: true,
    transform(file, encoding, callback) {
      for (let item in file) {
        console.log(item);
      }
      // console.log(file.extname);
      const contexts = file.contents.toString() + "\r\nconst { Transform } = require('stream');";
      // console.log(contexts);
      file.contents = Buffer.from(contexts);
      return callback(null, file)
    },
  });
}


const compose = () => {
  return through2.obj(function (file, enc, callback) {
    console.log(file.extname);
    // console.log(file.contents.toString());

    if (file.isNull()) {
      return callback(null, file);
    }

    if (file.isStream()) {
      return callback(createError(file, 'Streaming not supported'));
    }

    //do something
    file.contents = Buffer.from("\"this is my stream\";");

    callback(null, file);
  });
}

const build = () => {
  return src('./src/app.js').pipe(compose()).pipe(defineFn()).pipe(dest('./dist/app.js'));
}

module.exports = {
  build
}
